package com.flute.haflute.client;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * 需要传送的文件内容的封装对象<br>
 * 该对象的优势：一次包装和传输<br>
 * 缺陷：传送的一批文件大小不能太大，否则出错几率增大，影响网络和集群效率。
 * 
 */
public class TransferredFiles implements Serializable {
	
	private static final long serialVersionUID = -6825379640391842623L;
	
	private ArrayList<String> fileNames;
	private ArrayList<byte[]> fileContents;

	public TransferredFiles() {
		fileNames = new ArrayList<String>();
		fileContents = new ArrayList<byte[]>();
	}

	public TransferredFiles(String oneFileName) throws IOException {
		fileNames = new ArrayList<String>();
		fileContents = new ArrayList<byte[]>();
		addOneFile(oneFileName);
	}

	/**
	 * 增加需要传输的一个文件，该方法将同时读取文件数据，并缓存
	 * 
	 * @param fileName
	 *            文件名
	 * @throws IOException
	 */
	public void addOneFile(String fileName) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				fileName));
		byte[] buffer = new byte[4096];
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		int len = -1;
		while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
			bos.write(buffer, 0, len);
		}
		bos.close();
		bis.close();
		fileNames.add(fileName);
		fileContents.add(bos.toByteArray());
	}

	/**
	 * @return the fileNames
	 */
	public ArrayList<String> getFileNames() {
		return fileNames;
	}

	/**
	 * @return the fileContents
	 */
	public ArrayList<byte[]> getFileContents() {
		return fileContents;
	}

}
